package com.zretc.support.service.serviceimpl;

import com.sun.org.apache.regexp.internal.RE;
import com.zretc.support.dao.ReplyDao;
import com.zretc.support.entity.Reply;
import com.zretc.support.service.ReplyService;
import com.zretc.support.tool.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ReplyServiceImpl implements ReplyService {

    @Autowired
    private ReplyDao rd;

    @Autowired
    private PageData<Reply> pd;

    Logger logger = LoggerFactory.getLogger(ReplyServiceImpl.class);

    /**
     * 新增回复
     *
     * @param reply
     * @return
     */
    @Override
    public boolean addReply(Reply reply) {
        reply.setReplyStatus(1);
        if (rd.saveAndFlush(reply) != null) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据问题编号查询
     *
     * @param questionId
     * @param startTime
     * @param endTime
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageData<Reply> getReplyByQuestionIDAndPage(int questionId, Date startTime, Date endTime, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Reply> page1 = rd.getReplyByQuestionIDAndPage((long)questionId, startTime, endTime, pageable);
        pd.setCurrent(page1.getNumber());
        pd.setRecords(page1.getContent());
        pd.setTotal((int) page1.getTotalElements());
        pd.setSize(page1.getSize());
        pd.setCode(200);
        pd.setMsg("查询成功！");
        return pd;
    }

    //根据用户名模糊查询
    @Override
    public PageData<Reply> getReplyByUserNameAndPage(String userName, Date startTime, Date endTime, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Reply> page1 = rd.getReplyByUserNameAndPage(userName, startTime, endTime, pageable);
        pd.setCurrent(page1.getNumber());
        pd.setRecords(page1.getContent());
        pd.setTotal((int) page1.getTotalElements());
        pd.setSize(page1.getSize());
        pd.setCode(200);
        pd.setMsg("查询成功！");
        return pd;
    }

    @Override
    public PageData<Reply> getRepliesByTime(String replyContent, Date startTime, Date endTime, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Reply> page1 = rd.getRepliesByTime(replyContent, startTime, endTime, pageable);
        pd.setCurrent(page1.getNumber());
        pd.setRecords(page1.getContent());
        pd.setTotal((int) page1.getTotalElements());
        pd.setSize(page1.getSize());
        pd.setCode(200);
        pd.setMsg("查询成功！");
        return pd;
    }

    @Override
    public boolean delReplyById(Integer replyId) {
        Reply reply = new Reply();
        reply.setReplyId((long) replyId);
        try {
            long n1 = rd.count();
            rd.delete(reply);
            long n2 = rd.count();
            if (n1 == n2) {
                throw new NullPointerException();
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public Reply getReplyById(long replyId) {
        return rd.findReplyByReplyId(replyId);
    }

    @Override
    public boolean updateReply(Reply reply) {
        Reply reply1 = rd.saveAndFlush(reply);
        if (reply1 != null) {
            return true;
        } else {
            return false;
        }
    }

    //第一层的查询
    @Override
    public PageData<Reply> getReplyWithoutPid(long questionId, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Reply> page1 = rd.getReplyWithoutPid(questionId, pageable);
        pd.setCurrent(page1.getNumber());
        pd.setRecords(page1.getContent());
        pd.setTotal((int) page1.getTotalElements());
        pd.setSize(page1.getSize());
        pd.setCode(200);
        pd.setMsg("查询成功！");
        logger.info("impl:"+pd.getRecords());
        return pd;
    }

    //第二层的查询
    @Override
    public List<Reply> getReplyRe(int parentId) {
        return rd.getReplyRe(parentId);
    }
}
